package com.cn.lotary.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.*;

/**
 *
 *
 * @author Jason 14-Jul-2011
 */
public class EntityProvider_1 {

    public static String getType(String type) {
        String outcome = null;
        if ("INT".equals(type)) {
            outcome = "int";
        } else if ("VARCHAR".equals(type)) {
            outcome = "String";
        } else if ("DOUBLE".equals(type) || "BIT".equals(type)) {
            outcome = "Double";
        } else if ("TIMESTAMP".equals(type) || "DATETIME".equals(type)) {
            outcome = "Date";
        } else {
            System.err.println(type);
        }
        return outcome;
    }
    public static final String RN = "\r\n";

    public static void main(String[] args) throws IOException {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/customs";
        String user = "root";
        String password = "JasonChen";
        String packagePath = "com.cn.entity";
        String singleTable = null;
        Map<String, String> entitys = getEntitys(driver, url, user, password, packagePath, singleTable);
        for (String entity : entitys.keySet()) {
            makeClass("/home/user192/lotary/projects/lotaryBase/test/com/cn/entity", entity, entitys.get(entity));
        }
    }

    private static void makeClass(String entityPath, String entityName, String entity) throws IOException {

        File file = new File(entityPath);
        if (!file.exists()) {
            file.mkdirs();
            System.out.println("create the path [" + entityPath + "]");
        }
        String newfullpath = entityPath + "/" + entityName + ".java";

        File newfile = new File(newfullpath);
        if (!newfile.exists()) {
            newfile.createNewFile();
        }

        FileWriter fileWriter = new FileWriter(newfile, true);

        fileWriter.write(entity);
        fileWriter.close();
    }

    public static Map<String, String> getEntitys(String driver, String url, String user, String password, String packagePath, String singleTable) {
        List<Table> tables = getTables(driver, url, user, password, singleTable);

        Map<String, String> entitys = new HashMap<String, String>();
        for (Table table : tables) {
            boolean token = false;
            for (PrimaryKey key : table.getPrimaryKeys()) {
                String primaryKeyName = key.getColumnName();
                String fkName = null;
                for (ImportedKey ik : table.getImportedKeys()) {

                    fkName = ik.getFkcolumnName();
                    token = primaryKeyName.equals(fkName);
                    if (token) {
                        break;
                    }
                }

                if (token) {
                    System.err.println(primaryKeyName);
                    System.err.println(fkName);
                    break;
                }
            }
            if (token) {
                continue;
            }

            StringBuilder entity = new StringBuilder();
            entity.append("package " + packagePath + ";").append(RN);
            entity.append("import com.cn.lotary.base.dao.AbstractEntity;").append(RN);
            entity.append("import javax.persistence.*;").append(RN);

            for (Column key : table.getColumns()) {
                if (!key.getNullable()) {
                    entity.append("import javax.validation.constraints.NotNull;").append(RN);
                    break;
                }
            }
            if (!table.getExportedKeys().isEmpty()) {
                entity.append("import java.util.ArrayList;").append(RN);
                entity.append("import java.util.List;").append(RN);
                entity.append("import javax.xml.bind.annotation.XmlRootElement;").append(RN);
                entity.append("import javax.xml.bind.annotation.XmlTransient;").append(RN);
            }
            if (table.toString().contains("Date")) {
                entity.append("import java.util.Date;").append(RN);
            }

            String entityName = ObjectUtil.tableNameToEntityName(table.getName());
            entity.append("@Entity").append(RN);
            entity.append("@Table(name = \"" + table.getName() + "\")").append(RN);
            if (!table.getExportedKeys().isEmpty()) {
                entity.append("@XmlRootElement").append(RN);
            }
            entity.append("public class " + entityName + " extends AbstractEntity {").append(RN);
            entity.append("private static final long serialVersionUID = 1L;").append(RN);
            Map<String, String> allColmuns = new HashMap<String, String>();
            // System.out.println(table);
            for (PrimaryKey key : table.getPrimaryKeys()) {
                String columnName = key.getColumnName();
                entity.append("@Id").append(RN);
                entity.append("@GeneratedValue(strategy = GenerationType.IDENTITY)").append(RN);
                entity.append("@Column(name = \"" + columnName + "\", nullable = false)").append(RN);
                entity.append("private Integer " + columnName + ";").append(RN);
                allColmuns.put(columnName, "Integer");
            }

            for (Column column : table.getColumns()) {
                String columnName = column.getName();
                String type = column.getTypeName();
                allColmuns.put(ObjectUtil.columnNameToFieldName(columnName), type);

                entity.append("@Column(name = \"" + columnName + "\"");
                if (!"Double".equals(type) && !"Date".equals(type)) {
                    entity.append(", nullable = " + column.getNullable() + ", length = " + column.getSize());
                }
                entity.append(")").append(RN);
                if ("Date".equals(type)) {
                    entity.append("@Temporal(TemporalType.TIMESTAMP)").append(RN);
                }
                entity.append("private " + type + " " + ObjectUtil.columnNameToFieldName(columnName) + ";").append(RN);
                //System.out.println(column.getType());
            }
            for (ImportedKey key : table.getImportedKeys()) {
                String columnName = key.getPktableName();
                String pkEntityName = ObjectUtil.tableNameToEntityName(columnName);
                entity.append("@JoinColumn(name = \"" + columnName + "\", referencedColumnName = \"" + key.getPkcolumnName() + "\", nullable = " + key.isNullable() + ")").append(RN);
                entity.append("@ManyToOne").append(RN);
                if (!key.isNullable()) {
                    entity.append("@NotNull").append(RN);
                }
                entity.append("private " + pkEntityName + " " + ObjectUtil.toLowerCase(pkEntityName) + ";").append(RN);
                allColmuns.put(ObjectUtil.toLowerCase(pkEntityName), ObjectUtil.toUpperCase(pkEntityName));
            }
            for (ExportedKey key : table.getExportedKeys()) {
                String columnName = key.getFkColumnName();
                String tableName = ObjectUtil.tableNameToEntityName(key.getFkTableName());
                allColmuns.put(ObjectUtil.toLowerCase(tableName) + "s", "List<" + tableName + ">");

                entity.append("@OneToMany(cascade = CascadeType.ALL, mappedBy = \"" + columnName + "\", fetch = FetchType.LAZY)").append(RN);
                entity.append("private List<" + tableName + "> " + ObjectUtil.toLowerCase(tableName) + "s;").append(RN);
            }
            for (String key : allColmuns.keySet()) {
                String type = allColmuns.get(key);
                if (type != null && type.contains("List")) {
                    entity.append("@XmlTransient").append(RN);
                }
                entity.append("public " + type + " get" + ObjectUtil.toUpperCase(key) + "(){").append(RN);

                if (type != null && type.contains("List")) {
                    entity.append("if(" + key + " == null){").append(RN);
                    entity.append(key + " = new ArrayList<" + type.replace("List<", "").replace(">", "") + ">();").append(RN);
                    entity.append("}").append(RN);
                }
                entity.append("return " + key + ";").append(RN);
                entity.append("}").append(RN);

                entity.append("public void set" + ObjectUtil.toUpperCase(key) + "(" + type + " " + key + "){").append(RN);
                entity.append("this." + key + "=" + key + ";").append(RN);
                entity.append("}").append(RN);
            }



            entity.append("@Override").append(RN);
            entity.append("public int hashCode() {").append(RN);
            entity.append("int hash = 0;").append(RN);
            entity.append("hash += (id != null ? id.hashCode() : 0);").append(RN);
            entity.append("return hash;").append(RN);
            entity.append("}").append(RN);
            entity.append(RN);

            entity.append("@Override").append(RN);
            entity.append("public boolean equals(Object object) {").append(RN);
            entity.append("if (!(object instanceof " + entityName + ")) {").append(RN);
            entity.append("return false;").append(RN);
            entity.append("}").append(RN);
            entity.append(entityName + " other = (" + entityName + ") object;").append(RN);
            entity.append("if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {").append(RN);
            entity.append("return false;").append(RN);
            entity.append("}").append(RN);
            entity.append("return true;").append(RN);
            entity.append("}").append(RN);

            entity.append("}");
            entitys.put(entityName, entity.toString());
            //break;
        }
        return entitys;
    }

    private static List<Table> getTables(String driver, String url, String user, String password, String singleTable) {
        Connection con = null;

        List<Table> tabless = new ArrayList<Table>();
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            DatabaseMetaData databaseMetaData = con.getMetaData();
            ResultSet tables = databaseMetaData.getTables(null, "%", singleTable != null ? singleTable : "%", new String[]{"TABLE"});

            while (tables.next()) {
                String tableName = tables.getString("TABLE_NAME");

                List<ExportedKey> exportedKeyss = new ArrayList<ExportedKey>();
                ResultSet exportedKeys = databaseMetaData.getExportedKeys(null, null, tableName);
                while (exportedKeys.next()) {
//                    System.err.println(exportedKeys.getString("PKTABLE_CAT"));
//                    System.err.println(exportedKeys.getString("PKTABLE_SCHEM"));
//                    System.err.println("主键表名" + exportedKeys.getString("PKTABLE_NAME"));
//                    System.err.println("主键列名" + exportedKeys.getString("PKCOLUMN_NAME"));
//                    System.err.println(exportedKeys.getString("FKTABLE_CAT"));
//                    System.err.println(exportedKeys.getString("FKTABLE_SCHEM"));
//                    System.err.println("外键表名" + exportedKeys.getString("FKTABLE_NAME"));
//                    System.err.println("外键列名" + exportedKeys.getString("FKCOLUMN_NAME"));
//                    System.err.println(exportedKeys.getString("KEY_SEQ"));
//                    System.err.println("外键会发生什么事，主键是更新时：" + exportedKeys.getString("UPDATE_RULE"));
//                    System.err.println("外键会发生什么事，主键是删除时：" + exportedKeys.getString("DELETE_RULE"));
//                    System.err.println("外键的名称" + exportedKeys.getString("FK_NAME"));
//                    System.err.println("主键的名称" + exportedKeys.getString("PK_NAME"));
//                    System.err.println(exportedKeys.getString("DEFERRABILITY"));
                    ExportedKey exportedKey = new ExportedKey(
                            exportedKeys.getString("FKTABLE_NAME"),
                            exportedKeys.getString("FKCOLUMN_NAME"));
                    exportedKeyss.add(exportedKey);
                }


                ResultSet importedKeys = databaseMetaData.getImportedKeys(null, null, tableName);
                List<ImportedKey> importedKeyss = new ArrayList<ImportedKey>();
                List<String> importedKeyssName = new ArrayList<String>();
                while (importedKeys.next()) {
                    ImportedKey importedKey = new ImportedKey(
                            importedKeys.getString("PKTABLE_CAT"),
                            importedKeys.getString("PKTABLE_SCHEM"),
                            importedKeys.getString("PKTABLE_NAME"),
                            importedKeys.getString("PKCOLUMN_NAME"),
                            importedKeys.getString("FKTABLE_CAT"),
                            importedKeys.getString("FKTABLE_SCHEM"),
                            importedKeys.getString("FKTABLE_NAME"),
                            importedKeys.getString("FKCOLUMN_NAME"),
                            importedKeys.getString("KEY_SEQ"),
                            importedKeys.getString("UPDATE_RULE"),
                            importedKeys.getString("DELETE_RULE"),
                            importedKeys.getString("FK_NAME"),
                            importedKeys.getString("PK_NAME"),
                            importedKeys.getString("DEFERRABILITY"));
                    importedKeyss.add(importedKey);
                    importedKeyssName.add(importedKeys.getString("PKTABLE_NAME"));
                }

                ResultSet primaryKeys = databaseMetaData.getPrimaryKeys(null, null, tableName);
                List<PrimaryKey> primaryKeyss = new ArrayList<PrimaryKey>();
                List<String> primaryKeyssName = new ArrayList<String>();
                while (primaryKeys.next()) {
                    PrimaryKey primaryKey = new PrimaryKey(primaryKeys.getString("TABLE_CAT"),
                            primaryKeys.getString("TABLE_SCHEM"),
                            primaryKeys.getString("TABLE_NAME"),
                            primaryKeys.getString("COLUMN_NAME"),
                            primaryKeys.getString("KEY_SEQ"),
                            primaryKeys.getString("PK_NAME"));
                    primaryKeyss.add(primaryKey);
                    primaryKeyssName.add(primaryKeys.getString("COLUMN_NAME"));
                }

                String tableComment = tables.getString("REMARKS");
                String TABLE_CAT = tables.getString("TABLE_CAT");
                String TABLE_SCHEM = tables.getString("TABLE_SCHEM");
                String TABLE_TYPE = tables.getString("TABLE_TYPE");
                String REMARKS = tables.getString("REMARKS");
                //  String TYPE_CAT = tables.getString("TYPE_CAT");
                // String TYPE_SCHEM = tables.getString("TYPE_SCHEM");
                //  String TYPE_NAME = tables.getString("TYPE_NAME");
                // String SELF_REFERENCING_COL_NAME = tables.getString("SELF_REFERENCING_COL_NAME");
                // String REF_GENERATION = tables.getString("REF_GENERATION");

                ResultSet columns = databaseMetaData.getColumns(null, "%", tableName, "%");
                List<Column> columnss = new ArrayList<Column>();
                while (columns.next()) {
                    String columnName = columns.getString("COLUMN_NAME");
                    String NULLABLE = columns.getString("NULLABLE");
                    if (importedKeyssName.contains(columnName) || primaryKeyssName.contains(columnName)) {

                        for (ImportedKey key : importedKeyss) {
                            ImportedKey importedKey = key;
                            if (key.getFkName().equals(columnName)) {
                                importedKey.setNullable(Boolean.valueOf(NULLABLE));
                                importedKeyss.remove(key);
                                importedKeyss.add(importedKey);
                            }
                        }
                        continue;
                    }
                    String columnComment = columns.getString("REMARKS");
                    String columnType = columns.getString("DATA_TYPE");
                    String columnTYPE_NAME = getType(columns.getString("TYPE_NAME"));
                    String COLUMN_SIZE = columns.getString("COLUMN_SIZE");
                    String DECIMAL_DIGITS = columns.getString("DECIMAL_DIGITS");
                    String NUM_PREC_RADIX = columns.getString("NUM_PREC_RADIX");
                    String COLUMN_DEF = columns.getString("COLUMN_DEF");
                    String SQL_DATA_TYPE = columns.getString("SQL_DATA_TYPE");

                    String SQL_DATETIME_SUB = columns.getString("SQL_DATETIME_SUB");
                    String CHAR_OCTET_LENGTH = columns.getString("CHAR_OCTET_LENGTH");
                    String ORDINAL_POSITION = columns.getString("ORDINAL_POSITION");
                    String IS_NULLABLE = columns.getString("IS_NULLABLE");
                    //String SCOPE_CATLOG = columns.getString("SCOPE_CATLOG");
                    String SCOPE_SCHEMA = columns.getString("SCOPE_SCHEMA");
                    String SCOPE_TABLE = columns.getString("SCOPE_TABLE");
                    String SOURCE_DATA_TYPE = columns.getString("SOURCE_DATA_TYPE");
                    String IS_AUTOINCREMENT = columns.getString("IS_AUTOINCREMENT");
                    Column column = new Column(columnName, columnType, columnComment, columnTYPE_NAME,
                            COLUMN_SIZE, DECIMAL_DIGITS, NUM_PREC_RADIX, NULLABLE,
                            COLUMN_DEF, SQL_DATA_TYPE, SQL_DATETIME_SUB,
                            CHAR_OCTET_LENGTH, ORDINAL_POSITION, IS_NULLABLE,
                            SCOPE_SCHEMA, SCOPE_TABLE, SOURCE_DATA_TYPE, IS_AUTOINCREMENT);
                    columnss.add(column);
                }
                Table table = new Table(tableName, tableComment, TABLE_CAT, TABLE_SCHEM, TABLE_TYPE, REMARKS,
                        columnss, importedKeyss,
                        exportedKeyss,
                        primaryKeyss);
                tabless.add(table);

                if (tableName.equals("permission_has_module")) {
                    System.out.println(table);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return tabless;
    }

    private static class Table {

        private String name;
        private String comment;
        private String cat;
        private String schem;
        private String type;
        private String remarks;
        private List<Column> columns;
        private List<PrimaryKey> primaryKeys;
        private List<ImportedKey> importedKeys;
        private List<ExportedKey> exportedKeys;

        @Override
        public String toString() {
            return "Table{" + "name=" + name + ", comment=" + comment
                    + ", cat=" + cat + ", schem=" + schem + ", type="
                    + type + ", remarks=" + remarks
                    + ", \r\ncolumns="
                    + columns
                    + ",\r\nprimaryKeys="
                    + primaryKeys
                    + ",\r\nexportedKeys="
                    + exportedKeys
                    + ",\r\nimportedKeys="
                    + importedKeys + '}';
        }

        public Table(String tableName, String tableComment, String TABLE_CAT,
                String TABLE_SCHEM, String TABLE_TYPE, String REMARKS,
                List<Column> columns,
                List<ImportedKey> importedKeys,
                List<ExportedKey> exportedKeys,
                List<PrimaryKey> primaryKeys) {
            this.name = tableName;
            this.comment = tableComment;
            this.cat = TABLE_CAT;
            this.schem = TABLE_SCHEM;
            this.type = TABLE_TYPE;
            this.remarks = REMARKS;
            this.columns = columns;
            this.importedKeys = importedKeys;
            this.primaryKeys = primaryKeys;
            this.exportedKeys = exportedKeys;

        }

        public String getCat() {
            return cat;
        }

        public List<ImportedKey> getImportedKeys() {
            return importedKeys;
        }

        public List<Column> getColumns() {
            return columns;
        }

        public List<PrimaryKey> getPrimaryKeys() {
            return primaryKeys;
        }

        public List<ExportedKey> getExportedKeys() {
            return exportedKeys;
        }

        public String getComment() {
            return comment;
        }

        public String getName() {
            return name;
        }

        public String getRemarks() {
            return remarks;
        }

        public String getSchem() {
            return schem;
        }

        public String getType() {
            return type;
        }
    }

    private static class Column {

        private String name;
        private String type;
        private String comment;
        private String typeName;
        private String size;
        private String decimalDigits;
        private String numPrecRadix;
        private boolean nullable;
        private String columnDef;
        private String sqlDataType;
        private String sqlDatetimeSub;
        private String charOctetLength;
        private String ordinalPosition;
        private String isNullable;
        private String scopeSchema;
        private String scopeTable;
        private String SourceDataType;
        private String isAutoincrement;

        @Override
        public String toString() {
            return "Column{" + "name=" + name + ", type=" + type + ", comment=" + comment + ", typeName=" + typeName + ", size=" + size + ", decimalDigits=" + decimalDigits + ", numPrecRadix=" + numPrecRadix + ", nullable=" + nullable + ", columnDef=" + columnDef + ", sqlDataType=" + sqlDataType + ", sqlDatetimeSub=" + sqlDatetimeSub + ", charOctetLength=" + charOctetLength + ", ordinalPosition=" + ordinalPosition + ", isNullable=" + isNullable + ", scopeSchema=" + scopeSchema + ", scopeTable=" + scopeTable + ", SourceDataType=" + SourceDataType + ", isAutoincrement=" + isAutoincrement + "}\r\n";
        }

        public Column(String name) {
            this.name = name;
        }

        public Column(String name, String type, String comment, String columnTYPE_NAME, String COLUMN_SIZE, String DECIMAL_DIGITS, String NUM_PREC_RADIX, String NULLABLE, String COLUMN_DEF, String SQL_DATA_TYPE, String SQL_DATETIME_SUB, String CHAR_OCTET_LENGTH, String ORDINAL_POSITION, String IS_NULLABLE, String SCOPE_SCHEMA, String SCOPE_TABLE, String SOURCE_DATA_TYPE, String IS_AUTOINCREMENT) {
            this.name = name;
            this.type = type;
            this.comment = comment;
            this.typeName = columnTYPE_NAME;
            this.size = COLUMN_SIZE;
            this.decimalDigits = DECIMAL_DIGITS;
            this.numPrecRadix = NUM_PREC_RADIX;
            this.nullable = Boolean.valueOf(NULLABLE);
            this.columnDef = COLUMN_DEF;
            this.sqlDataType = SQL_DATA_TYPE;
            this.sqlDatetimeSub = SQL_DATETIME_SUB;
            this.charOctetLength = CHAR_OCTET_LENGTH;
            this.ordinalPosition = ORDINAL_POSITION;
            this.isNullable = IS_NULLABLE;
            this.scopeSchema = SCOPE_SCHEMA;
            this.scopeTable = SCOPE_TABLE;
            this.SourceDataType = SOURCE_DATA_TYPE;
            this.isAutoincrement = IS_AUTOINCREMENT;
        }

        public String getSourceDataType() {
            return SourceDataType;
        }

        public String getCharOctetLength() {
            return charOctetLength;
        }

        public String getColumnDef() {
            return columnDef;
        }

        public String getComment() {
            return comment;
        }

        public String getDecimalDigits() {
            return decimalDigits;
        }

        public String getIsAutoincrement() {
            return isAutoincrement;
        }

        public String getIsNullable() {
            return isNullable;
        }

        public String getName() {
            return name;
        }

        public boolean getNullable() {
            return nullable;
        }

        public String getNumPrecRadix() {
            return numPrecRadix;
        }

        public String getOrdinalPosition() {
            return ordinalPosition;
        }

        public String getScopeSchema() {
            return scopeSchema;
        }

        public String getScopeTable() {
            return scopeTable;
        }

        public String getSize() {
            return size;
        }

        public String getSqlDataType() {
            return sqlDataType;
        }

        public String getSqlDatetimeSub() {
            return sqlDatetimeSub;
        }

        public String getType() {
            return type;
        }

        public String getTypeName() {
            return typeName;
        }
    }

    private static class PrimaryKey {

        private String tableCat;
        private String tableSchem;
        private String tableName;
        private String columnName;
        private String keySeq;
        private String pkName;

        public PrimaryKey(String TABLE_CAT, String TABLE_SCHEM, String TABLE_NAME, String COLUMN_NAME, String KEY_SEQ, String PK_NAME) {
            this.tableCat = TABLE_CAT;
            this.tableSchem = TABLE_SCHEM;
            this.tableName = TABLE_NAME;
            this.columnName = COLUMN_NAME;
            this.keySeq = KEY_SEQ;
            this.pkName = PK_NAME;
        }

        @Override
        public String toString() {
            return "PrimaryKey{"
                    + "tableCat=" + tableCat + ", tableSchem="
                    + tableSchem + ", tableName=" + tableName + ", columnName="
                    + columnName + ", keySeq=" + keySeq + ", pkName=" + pkName + '}';
        }

        public String getColumnName() {
            return columnName;
        }

        public String getKeySeq() {
            return keySeq;
        }

        public String getPkName() {
            return pkName;
        }

        public String getTableCat() {
            return tableCat;
        }

        public String getTableName() {
            return tableName;
        }

        public String getTableSchem() {
            return tableSchem;
        }
    }

    private static class ImportedKey {

        private String pktableCat;
        private String pktableSchem;
        private String pktableName;
        private String pkcolumnName;
        private String fktableCat;
        private String fktableSchem;
        private String fktableName;
        private String fkcolumnName;
        private String keySeq;
        private String updateRule;
        private String deleteRule;
        private String fkName;
        private String pkName;
        private String deferrability;
        private boolean nullable;

        public ImportedKey(String PKTABLE_CAT, String PKTABLE_SCHEM,
                String PKTABLE_NAME,
                String PKCOLUMN_NAME, String FKTABLE_CAT,
                String FKTABLE_SCHEM, String FKTABLE_NAME,
                String FKCOLUMN_NAME, String KEY_SEQ,
                String UPDATE_RULE, String DELETE_RULE,
                String FK_NAME, String PK_NAME, String DEFERRABILITY) {
            this.pktableCat = PKTABLE_CAT;
            this.pktableSchem = PKTABLE_SCHEM;
            this.pktableName = PKTABLE_NAME;
            this.pkcolumnName = PKCOLUMN_NAME;
            this.fktableCat = FKTABLE_CAT;
            this.fktableSchem = FKTABLE_SCHEM;
            this.fktableName = FKTABLE_NAME;
            this.fkcolumnName = FKCOLUMN_NAME;
            this.keySeq = KEY_SEQ;
            this.updateRule = UPDATE_RULE;
            this.deleteRule = DELETE_RULE;
            this.fkName = FK_NAME;
            this.pkName = PK_NAME;
            this.deferrability = DEFERRABILITY;
        }

        @Override
        public String toString() {
            return "ImportedKey{" + "pktableCat=" + pktableCat + ", pktableSchem=" + pktableSchem + ", pktableName=" + pktableName + ", pkcolumnName=" + pkcolumnName + ", fktableCat=" + fktableCat + ", fktableSchem=" + fktableSchem + ", fktableName=" + fktableName + ", fkcolumnName=" + fkcolumnName + ", keySeq=" + keySeq + ", updateRule=" + updateRule + ", deleteRule=" + deleteRule + ", fkName=" + fkName + ", pkName=" + pkName + ", deferrability=" + deferrability + '}';
        }

        public boolean isNullable() {
            return nullable;
        }

        public void setNullable(boolean nullable) {
            this.nullable = nullable;
        }

        public String getDeferrability() {
            return deferrability;
        }

        public String getDeleteRule() {
            return deleteRule;
        }

        public String getFkName() {
            return fkName;
        }

        public String getFkcolumnName() {
            return fkcolumnName;
        }

        public String getFktableCat() {
            return fktableCat;
        }

        public String getFktableName() {
            return fktableName;
        }

        public String getFktableSchem() {
            return fktableSchem;
        }

        public String getKeySeq() {
            return keySeq;
        }

        public String getPkName() {
            return pkName;
        }

        public String getPkcolumnName() {
            return pkcolumnName;
        }

        public String getPktableCat() {
            return pktableCat;
        }

        public String getPktableName() {
            return pktableName;
        }

        public String getPktableSchem() {
            return pktableSchem;
        }

        public String getUpdateRule() {
            return updateRule;
        }
    }

    private static class ExportedKey {

        private String fkTableName;
        private String fkColumnName;

        public ExportedKey(String fkTableName, String fkColumnName) {
            this.fkTableName = fkTableName;
            this.fkColumnName = fkColumnName;
        }

        @Override
        public String toString() {
            return "ExportedKey{" + "fkTableName=" + fkTableName + ", fkColumnName=" + fkColumnName + '}';
        }

        public String getFkColumnName() {
            return fkColumnName;
        }

        public String getFkTableName() {
            return fkTableName;
        }
    }
}
